package com.danciyixue.boot.dcyx.controller;

import com.danciyixue.boot.dcyx.service.TestRecordsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.danciyixue.boot.dcyx.model.form.TestRecordsForm;
import com.danciyixue.boot.dcyx.model.query.TestRecordsQuery;
import com.danciyixue.boot.dcyx.model.vo.TestRecordsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.danciyixue.boot.common.result.PageResult;
import com.danciyixue.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 测试结果记录前端控制层
 *
 * @author youlaitech
 * @since 2025-06-15 16:31
 */
@Tag(name = "测试结果记录接口")
@RestController
@RequestMapping("/api/v1/test-records")
@RequiredArgsConstructor
public class TestRecordsController  {

    private final TestRecordsService testRecordsService;

    @Operation(summary = "测试结果记录分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('dcyx:test-records:query')")
    public PageResult<TestRecordsVO> getTestRecordsPage(TestRecordsQuery queryParams ) {
        IPage<TestRecordsVO> result = testRecordsService.getTestRecordsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增测试结果记录")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('dcyx:test-records:add')")
    public Result<Void> saveTestRecords(@RequestBody @Valid TestRecordsForm formData ) {
        boolean result = testRecordsService.saveTestRecords(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取测试结果记录表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('dcyx:test-records:edit')")
    public Result<TestRecordsForm> getTestRecordsForm(
        @Parameter(description = "测试结果记录ID") @PathVariable Long id
    ) {
        TestRecordsForm formData = testRecordsService.getTestRecordsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改测试结果记录")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('dcyx:test-records:edit')")
    public Result<Void> updateTestRecords(
            @Parameter(description = "测试结果记录ID") @PathVariable Long id,
            @RequestBody @Validated TestRecordsForm formData
    ) {
        boolean result = testRecordsService.updateTestRecords(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除测试结果记录")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('dcyx:test-records:delete')")
    public Result<Void> deleteTestRecordss(
        @Parameter(description = "测试结果记录ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = testRecordsService.deleteTestRecordss(ids);
        return Result.judge(result);
    }
}
